<!-- 采样点项目 -->
<template>
	<view class="index">
		
		 <!-- #ifdef APP-PLUS || MP-WEIXIN -->
		<view class="press-tips">点击输入行上边的名称可语音输入</view>
		
		<!-- #endif -->
		

		<view class="uni-list">
			<label-input v-for="(item, index) of data.common" ref="mpvuePicker" :key="index" :data="item" :alldata="data.common"
			 :inkey="index"></label-input>

			<view class="uni-list-cell-divider">监测点</view>

			<!-- <view>
        <view
          class="uni-list-cell"
          hover-class="uni-list-cell-hover"
          v-for="(item, key) of data.cyd_info"
          :key="key"
          @click="goSamplingPoint(key)"
        >
          <view class="uni-list-cell-navigate uni-navigate-right">
            <label>监测点 : {{1212}}</label>
          </view>
        </view>
      </view> -->
			<view>
				<view class="uni-list-cell" hover-class="uni-list-cell-hover" v-for="(item, key) of sendArr" :key="key" @click="goSamplingPoint(item.id,item.name)">
					<view class="uni-list-cell-navigate uni-navigate-right">
						<!-- <label>{{item}}</label> -->
						<label>监测点 : {{item.name?item.name:''}}</label>
					</view>
				</view>
			</view>

			<!-- <view>
				<view class="uni-list-cell" hover-class="uni-list-cell-hover" v-for="(item, key) of listData" :key="key"
				 @click="goSamplingPoint(key+1)">
					<view class="uni-list-cell-navigate uni-navigate-right">
						<label>监测点 : {{item.location_id}}</label>
					</view>
				</view>
      </view>-->
		</view>

		<view class="btn-row uni-flex">
			<button type="primary" class="primary uni-flex-item" style="margin-right: 10upx;" @tap="save">保存</button>
			<button type="primary" class="primary uni-flex-item" @tap="saveSubmit">保存并上传</button>
		</view>
	</view>
</template>

<script>
	import net from "../../net.js";
	import service from "../../service.js";
	import utils from "../../common/utils.js";
	import labelInput from "../../components/label-input.vue";
	import uploaddata from "../../uploaddata.js";

	export default {
		components: {
			labelInput
		},
		data() {
			return {
				data: {
					common: {}
				},
				codeId: 0,
				tableId: 0, // 表 id
				dayNum: 0, // 天数
				//isShowSamplingPoint: false
				newId: [],
				// 渲染页面的新数组 
				sendArr: [],
				//新的采样点id
				info_Id: {},
			};
		},
		onLoad(res) {
			//console.log(res, "_____我是res");
			this.codeId = res.codeId;
			this.tableId = res.tableId;
			this.dayNum = res.dayNum;
			console.log(res);
			let data = service.getTaskDetailDay(this.codeId, this.tableId, this.dayNum);

			Object.assign(this.data, data);
			 console.log(this.data, "___________我是dagnum里面的data");
			console.log(this.data.cyd_info, "_____我是cyd");
			for (let key in this.data.cyd_info) {

				let keyArr = Object.keys(this.data.cyd_info[key]);
				this.getName(keyArr[0])
				// this.info_Id[key] = keyArr[0]				
			}


		},
		onShow() {
			this.newId.forEach(v => {
				this.getName(v)
			})


		},
		onReady() {
			this.initItem();
			console.log("onReady");
		},
		methods: {
			getName(id) {
				let _this = this;
				net.netGet(
					"/module/api/main/getEditSampleInfo/" + encodeURIComponent(id), {},
					res => {
						console.log(res, "getName______res");
						let newObj = _this.data.cyd_info;
						console.log(newObj)
						// 循环对象
						for (let key in newObj) {
							//console.log(_this.data.cyd_info[key]);
							for (let key1 in newObj[key]) {
								// console.log(key1, "woshi_____key1")

								// 获取对象里的 id 和请求回来的 id 相匹配情况下 => 把名字和 key 值作为 Object 添加到 为新数组里
								if (key1 === res.id) {
									let obj = {};
									obj.id = key;
									obj.name = res.location_id;
									this.sendArr.push(obj);
								}
							}
						}
						

						function compare(property) {
							return function(a, b) {
								var value1 = a[property];
								var value2 = b[property];
								return value1 - value2;
							}
						}
						this.sendArr.sort(compare('id'))
						console.log(this.sendArr, "_____sendArr")
					}
				);


			},
			// 采样点
			goSamplingPoint(samplingPointId, samplingPointName) {
				console.log(samplingPointId, "我是samplingPointId");
				console.log(samplingPointName, "我是samplingPointNAme");
				uni.setStorageSync('samplingPointId', this.sendArr)
				//   return;
				let param = utils.toParam({
					codeId: this.codeId,
					tableId: this.tableId,
					dayNum: this.dayNum,
					// 被点击的 key 值
					samplingPointId: samplingPointId,
					// 被点击的名字
					samplingPointName: samplingPointName,
					// 把新数组传递过去
					transmitArr: this.sendArr,

				});
				// 用微信开发者工具调试时,它最大支持5个页面,需要把下面注释掉
				// 其它的
				//#ifdef H5 ||APP-PLUS
				uni.navigateTo({
					url: "./sampling_item_new" + param
				});
				// #endif
				// 用微信开发者工具调试时,它最大支持5个页面,需要把下面注释放开
				//#ifdef MP-WEIXIN
				uni.redirectTo({
					url: "./sampling_item_new" + param
				});
				// #endif
			},
			// 初始化 需要填写的内容
			initItem() {
				setTimeout(() => {
					this.$refs.mpvuePicker &&
						this.$refs.mpvuePicker.forEach((item, index) => {
							item.init();
						});
				}, 0);
			},
			save() {
				this.saveData();
			},
			saveSubmit() {
				this.saveData(true);
			},
			saveData(isSubmit = false) {
				uni.showLoading({
					title: "",
					mask: true
				});
				service.setTaskDetailDay(
					this.codeId,
					this.tableId,
					this.dayNum,
					this.data,
					"common"
				);
				utils.exeMainCallBack({}, "上传");
				if (!isSubmit) {
					uni.hideLoading();
					uni.showToast({
						icon: "none",
						title: "保存成功"
					});
				} else {
					uploaddata.uploadSampleData(false, true);
				}
			}
		}
	};
</script>

<style>
	.hide {
		display: none;
	}

	.uni-list {
		margin: 10upx 0 60upx 0;
	}

	.uni-list-cell:after {
		left: 0;
	}

	.uni-list-cell {
		padding: 0 20upx;
		min-height: 90upx;
		color: #666;
	}

	.uni-list-cell.con-list {
		flex-wrap: wrap;
	}

	.uni-list-cell.con-list text {
		flex: 0 0 100%;
		font-size: 32upx;
	}

	.uni-list-cell-divider~.uni-list-cell {
		padding-left: 50upx;
		min-height: 70upx;
	}

	.uni-list-cell.no-left {
		padding-left: 20upx;
	}
</style>
